﻿Pager = function () {
    this.pageIndex = 1;

    this.pageSize = 10;

    //表示的分页数
    this.pageCount = 5;

    //排序列
    this.sortColumn = "";

    //排序规则默认asc
    this.sortMode = "asc";

    //table实体
    this.tableIns = null;
};

Pager.prototype = {
    //初期化処理
    init: function (tableIns, data) {
        this.tableIns = tableIns;
        this.pageIndex = data.PageIndex;
        this.pageSize = data.PageSize;
        this.pageCount = data.PageCount;
    },
    //创建分页div
    create: function () {
        var pageHTML = "";

        pageHTML += "<div style='padding-top: 10px; text-align: right;'><ul class='ul_class'>";

        if (this.pageIndex > 1) {
            pageHTML += "<li><a href='#' style='TEXT-DECORATION:none' onclick='pagerChangePage(\"" + this.tableIns.tableId + '","' + 1 + "\")' >|<<</a></li>";
            pageHTML += "<li><a href='#' style='TEXT-DECORATION:none' onclick='pagerChangePage(\"" + this.tableIns.tableId + '","' + (this.pageIndex - 1) + "\")' ><</a></li>";
        }

        var startPage = (this.pageIndex + 5 <= this.pageCount ? this.pageIndex : this.pageCount - 4);
        startPage = startPage <= 0 ? 1 : startPage;

        var endPage = startPage + 4;
        endPage = endPage > this.pageCount ? this.pageCount : endPage;

        //遍历页码
        for (var i = startPage; i <= endPage; i++) {
            var disabled = "";

            if (i == this.pageIndex) {
                disabled = "disabled='disabled' style='cursor:default'";
            }

            pageHTML += "<li><a href='#' onclick='pagerChangePage(\"" + this.tableIns.tableId + '","' + i + "\")' " + disabled + ">" + i + "</a></li>";
        }

        if (this.pageIndex != this.pageCount) {
            if (this.pageCount > 0) {
                pageHTML += "<li><a href='#' style='TEXT-DECORATION:none' onclick='pagerChangePage(\"" + this.tableIns.tableId + '","' + (this.pageIndex + 1) + "\")' >></a></li>";
                pageHTML += "<li><a href='#' style='TEXT-DECORATION:none' onclick='pagerChangePage(\"" + this.tableIns.tableId + '","' + this.pageCount + "\")' >>>|</a></li>";
            }
        }
        pageHTML += "</ul></div>";

        return pageHTML;
    }
};

//点击页码翻页功能绑定
function pagerChangePage(tableId, pageNo) {
    //得到table对象
    var tableIns = TableInsList[tableId];

    //页码赋值
    tableIns.pager.pageIndex = pageNo;

    //翻页回调函数
    if (typeof tableIns.changePageCallback == "function") {
        //调用table对象中的“changePageCallback”方法
        tableIns.changePageCallback();
    }
    return false;
}